/**
 * 
 */
package storyteller;

import com.flagstone.transform.FSDefineText2;
import com.flagstone.transform.FSMovie;

/**
 * @author Ale
 *
 */
public class Position
{
  public static String TOP = "top";
  public static String TOP_LEFT = "top-left";
  public static String TOP_RIGHT = "top-right";
  public static String MIDDLE = "middle";
  public static String MIDDLE_LEFT = "middle-left";
  public static String MIDDLE_RIGHT = "middle-right";
  public static String BOTTOM = "bottom";
  public static String BOTTOM_LEFT = "bottom-left";
  public static String BOTTOM_RIGHT = "bottom-right";
  
  public static int calculateX(FSDefineText2 text, FSMovie movie, String position, int margin, String defaultPosition)
  {
    position = defaultTo(position, defaultPosition);
    
    if(position.equalsIgnoreCase(TOP_LEFT) 
        || position.equalsIgnoreCase(MIDDLE_LEFT) 
        || position.equalsIgnoreCase(BOTTOM_LEFT))
    {
      return margin;
    }    
    else if(position.equalsIgnoreCase(TOP_RIGHT) 
        || position.equalsIgnoreCase(MIDDLE_RIGHT) 
        || position.equalsIgnoreCase(BOTTOM_RIGHT))
    {
      return movie.getFrameSize().getWidth() - text.getBounds().getWidth() - margin;
    }
    else // default to horizontally-centered text
    {
      return movie.getFrameSize().getWidth() / 2 - text.getBounds().getWidth() / 2;
    }    
  }
  
  public static int calculateY(FSDefineText2 text, FSMovie movie, String position, int margin, String defaultPosition)
  {
    position = defaultTo(position, defaultPosition);
    
    int y = 0;
     
    if(position.equalsIgnoreCase(TOP) 
        || position.equalsIgnoreCase(TOP_LEFT) 
        || position.equalsIgnoreCase(TOP_RIGHT))
    {      
      y = margin ;
    }    
    else if(position.equalsIgnoreCase(MIDDLE) 
        || position.equalsIgnoreCase(MIDDLE_LEFT) 
        || position.equalsIgnoreCase(MIDDLE_RIGHT))
    {
      y = movie.getFrameSize().getHeight()/2 - text.getBounds().getHeight()/2;
    }
    else
    {      
      y = movie.getFrameSize().getHeight() - text.getBounds().getHeight();
    }    
    
    // Text is rendered on coordinates different than the origin, so we must consider these values
    y = y - text.getBounds().getMinY();
    
    return y;
  }
  
  private static String defaultTo(String position, String defaultPosition)
  {
    if(position.equals(TOP) || position.equals(TOP_LEFT) || position.equals(TOP_RIGHT)
        || position.equals(MIDDLE) || position.equals(MIDDLE_LEFT) || position.equals(MIDDLE_RIGHT)
        || position.equals(BOTTOM) || position.equals(BOTTOM_LEFT) || position.equals(BOTTOM_RIGHT))
    {
      return position;
    }
    else
    {
      return defaultPosition;
    }
  }
}
